package com.xq.spring.postprocess.bean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
import org.springframework.beans.factory.config.BeanPostProcessor;

import com.xq.spring.beans.Chinese;

/**
 * 定制的Bean后处理器.
 * 
 * @author 郭凯强
 */
public class CustomBeanPostProcessor implements BeanPostProcessor {

	private final Marker marker = MarkerFactory.getMarker("BeanPostProcessor");
	private final Logger logger = LoggerFactory
			.getLogger(CustomBeanPostProcessor.class);

	/*
	 * 对容器中的Bean实例进行后处理.
	 * 
	 * @see org.springframework.beans.factory.config.BeanPostProcessor#
	 * postProcessBeforeInitialization(java.lang.Object, java.lang.String)
	 */
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) {
		logger.debug(
				marker,
				"{}before any bean initialization callbacks (like InitializingBean's afterPropertiesSet or a custom init-method)",
				"[DOC]");
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) {
		logger.debug(
				marker,
				"{}after any bean initialization callbacks (like InitializingBean's afterPropertiesSet or a custom init-method)",
				"[DOC]");
		if (bean instanceof Chinese) {
			Chinese chinese=(Chinese) bean;
			chinese.setName("猪八戒");
			logger.debug(marker,"Bean后处理器修改Chinese的name属性={}","猪八戒");
		}
		return bean;
	}

}
